﻿Public Class fCajas
    Dim daCajas As lbGlobalcom.dsFacturacionTableAdapters.FAC_CAJASTableAdapter
    Dim daClave As lbGlobalcom.dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
    Private Sub fCajas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daCajas = New lbGlobalcom.dsFacturacionTableAdapters.FAC_CAJASTableAdapter
            daClave = New lbGlobalcom.dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
            LLenaSedes()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub FAC_CAJASBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FAC_CAJASBindingNavigatorSaveItem.Click
        Try
            Validate()
            FAC_CAJASBindingSource.EndEdit()
            daCajas.Update(DsFacturacion.FAC_CAJAS)
            MessageBox.Show("Se salvarón registros con exito", "Salvar Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnCargarCajas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarCajas.Click
        Try
            If DsFacturacion.FAC_CAJAS.GetChanges IsNot Nothing Then
                FAC_CAJASBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    FAC_CAJASBindingNavigatorSaveItem.PerformClick()
                End If
            End If
            daCajas.Fill(DsFacturacion.FAC_CAJAS)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub FAC_CAJASDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles FAC_CAJASDataGridView.CellBeginEdit
        If e.ColumnIndex = 1 AndAlso FAC_CAJASDataGridView.Rows(e.RowIndex).Cells(0).Value Is DBNull.Value Then
            Dim dt As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASDataTable
            dt = daClave.GetDataByIdClavePrimaria("FAC_CAJAS")
            If dt.Count Then
                Dim dr As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASRow
                dr = dt.Rows(0)
                Dim Cons As lbGlobalcom.ConsecTablas = New lbGlobalcom.ConsecTablas(dr("TABLA"), dr("CAMPO"), dr("LONGITUD"))
                FAC_CAJASDataGridView.Rows(e.RowIndex).Cells(0).Value = Cons.NumeroConsec
                Cons = Nothing
                FAC_CAJASBindingSource.EndEdit()
                daCajas.Update(DsFacturacion.FAC_CAJAS)
            End If
        End If
    End Sub

    Private Sub FAC_CAJASDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles FAC_CAJASDataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub

    Private Sub LLenaSedes()
        Try
            Dim daSedes As New lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
            daSedes.Fill(Me.DsGlobal.GLO_SEDES)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub



    Private Sub FAC_CAJASDataGridView_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles FAC_CAJASDataGridView.UserDeletedRow
        Try
            daCajas.Update(DsFacturacion.FAC_CAJAS)
            MessageBox.Show("Se elimino registro con exito", "Elimina Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub FAC_CAJASDataGridView_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles FAC_CAJASDataGridView.UserDeletingRow
        If MessageBox.Show("Esta seguro que dese eliminar este registro", "Elimina Registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End Sub
End Class